package com.vidyu.businesslayer.businesscomponents;

import com.google.common.collect.Sets;
import com.vidyu.businesslayer.businessentities.UserInProject;
import com.vidyu.businesslayer.businessentities.Employer;
import com.vidyu.businesslayer.businessentities.Project;
import com.vidyu.businesslayer.businessentities.Status;
import com.vidyu.datalayer.dataaccesscomponents.UserInProjectDao;
import com.vidyu.datalayer.dataaccesscomponents.EmployerDao;
import com.vidyu.datalayer.dataaccesscomponents.ProjectDao;
import com.vidyu.servicelayer.servicedataentitties.UserInProjectRO;
import com.vidyu.servicelayer.servicedataentitties.ProjectRO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: eyalsa
 * Date: 4/6/13
 * Time: 3:30 PM
 * To change this template use File | Settings | File Templates.
 */
@Component
public class ProjectHandler {

    @Autowired
    private EmployerDao employerDao;

    @Autowired
    private ProjectDao projectDao;

    @Autowired
    private UserInProjectDao contractorInProjectDao;

    public long addProjectToEmployer(ProjectRO projectRO){
        Employer employer = employerDao.findById(projectRO.getEmployerId());
        Set<Project> projectSet = employer.getProjectSet();
        if (projectSet == null){
            projectSet = Sets.newHashSet();
        }


        Project project = new Project(projectRO.getTitle(), projectRO.getProjDesc(), projectRO.getBudget(), projectRO.getDaysDuration(),
                projectRO.getStartDate(), Status.ACTIVE,
                projectRO.getRequestedHours(), projectRO.getPrice(), projectRO.isPerHour(), projectRO.getProjTypeDesc());

        long id = projectDao.save(project);
        projectSet.add(project);
        employer.setProjectSet(projectSet);
        employerDao.update(employer);
        return id;
    }

    public void updateProject(ProjectRO projectRO){
        Project dbProj = projectDao.findById(projectRO.getId());
        dbProj.setProjectTypeDesc(projectRO.getProjTypeDesc());
        dbProj.setBudget(projectRO.getBudget());
        dbProj.setDaysDuration(projectRO.getDaysDuration());
        dbProj.setPerHour(projectRO.isPerHour());
        dbProj.setPrice(projectRO.getPrice());
        dbProj.setProjDesc(projectRO.getProjDesc());
        dbProj.setRequestedHours(projectRO.getRequestedHours());
        dbProj.setStartDate(projectRO.getStartDate());
        dbProj.setProjectTypeDesc(projectRO.getProjTypeDesc());
        dbProj.setTitle(projectRO.getTitle());
        dbProj.setStatus(Status.valueOf(projectRO.getStatus()));

        projectDao.update(dbProj);
    }

    public long contractorApplyToProject(UserInProjectRO userInProjectRO) {
        return contractorInProjectDao.save(new UserInProject(userInProjectRO.userId,
                userInProjectRO.projectId,
                UserInProject.ProjectStatus.APPLY));
    }

    public void updateUserInProject(UserInProjectRO userInProjectRO) {
        UserInProject dbUserInProject = contractorInProjectDao.findById(userInProjectRO.id);
        dbUserInProject.setContractorId(userInProjectRO.userId);
        dbUserInProject.setProjectId(userInProjectRO.projectId);
        dbUserInProject.setStatus(UserInProject.ProjectStatus.valueOf(userInProjectRO.status));
        contractorInProjectDao.update(dbUserInProject);
    }

    public List<UserInProject> getUsersInProjectByStatus(long projectId, String status){
        return  contractorInProjectDao.getUsersInProjectByStatus(projectId, UserInProject.ProjectStatus.valueOf(status));
    }

}
